package edu.cibertec.sicsolutions.util;

import java.util.List;
import java.util.Map;

 
public final class ObjectUtil {
	
	private ObjectUtil(){		
	}
	
	/**
	 * Valida si es <code>null</code> o "". Valido solo para List, Object[] y Map
	 * @param object
	 * @return
	 */
	public static boolean isNullOrEmpty(Object object){
		if (object != null) {
			if (object instanceof List) {
				return ((List)object).isEmpty();
			}else if(object instanceof Object[]){
				return ((Object[])object).length == 0;
			}else if(object instanceof Map){
				return ((Map)object).isEmpty();
			}else if(object instanceof String){
				return StringUtil.isNullOrEmpty((String)object);
			}
			return false;
		}
		return true;
	}

	/** 
	 * Obtiene el valor hascode de un objeto y devuelve cero si es <code>null</code>
	 * @param object
	 * @return
	 */
	public static int hashCode(java.lang.Object object) {
		return (object == null) ? 0 : object.hashCode();
	}

	/** 
	 * Determina si dos objetos son iguales, recordar que el objeto debe tener implementado el metodo equal
	 * <pre>
	 * ObjectUtil.equal(null, null)		= true
	 * ObjectUtil.equal(object1, null)	= false
	 * </pre>
	 * @param object1
	 * @param object2
	 * @return
	 */
	public static boolean equal(java.lang.Object object1, java.lang.Object object2) {
		return object1 == object2 || (object1 != null && object1.equals(object2));
	}
	
    /** 
     * Null safe Comparation
     * @param c1
     * @param c2
     * @return
     */
    public static Object min(Comparable c1, Comparable c2) {
        if (c1 != null && c2 != null) {
            return c1.compareTo(c2) < 1 ? c1 : c2;
        } else {
            return c1 != null ? c1 : c2;
        }                              
    }
    
    /** 
     * Null safe Comparation
     * @param c1
     * @param c2
     * @return
     */
    public static Object max(Comparable c1, Comparable c2) {
        if (c1 != null && c2 != null) {
            return c1.compareTo(c2) >= 0 ? c1 : c2;
        } else {
            return c1 != null ? c1 : c2;
        }
    }
    
    public static Object trimObject(Object object){
    	return object;
    }

}
